The goal of peRformance is to provide a range of functions to study and visualize market or economic data and analyize and benchmark asset returns.
You can install the development version of peRformance from GitHub with:
# install.packages("devtools")
devtools::install_github("fscheler/peRformance")
Create a monthly and annual performance overview table in plotly and a printable version that can be used in markdown documents with the kable package. Monthly tables are a popular tool in analysis and comparison of financial time series.
library(peRformance)
# Generate an integrated monthly and annual performance overview using plotly
date=seq(as.Date("2007-1-1"), as.Date("2015-1-1"), by = "days")
asset_ret<-rnorm(length(date))/100
benchmark_ret<-rnorm(length(date))/100
da<-data.frame(date,asset_ret,benchmark_ret)
# Including Benchmark Returns
df<-mperf_table(da,ts_format="returns")
#> Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct
#> 1 2007 0.01% 1.21% 2.82% -1.79% 1.24% 1.47% -2.93% -4.26% -3.95% 14.77%
#> 2 2008 -9.89% 4.23% 1% 0.23% -7.71% -2.82% 6.79% -1.31% 1.27% 1.49%
#> 3 2009 1.19% 1.3% -10.15% 1.04% -3.33% -0.09% -3.15% 8.15% 9.76% -0.86%
#> 4 2010 2.53% -4.61% 4.13% -7.95% 4.38% -7.6% 14.39% 1.64% -0.07% -4.13%
#> 5 2011 -6.52% 0.5% -6.92% -8.89% -3.13% -1.88% 4.38% 7.09% -3.68% 5.3%
#> 6 2012 -1.4% -3.02% -9.76% 1.24% 6.44% 1.93% 1.28% 7.13% 2.14% -8.75%
#> 7 2013 -0.33% 3.97% 2.91% 0.54% -5.59% 10.7% 9.52% -2.81% 8.69% 1.8%
#> 8 2014 -1.1% 4.38% -5.89% -0.46% 2.14% -4.72% -8.05% -4.09% 6.32% 13.7%
#> 9 2015 0.4%
#> Nov Dec FY Benchmark
#> 1 -4.32% 1.3% 4.27% 35.79%
#> 2 -3.8% -6.08% -16.56% 4.09%
#> 3 2.87% -8.4% -3.47% 7.07%
#> 4 -0.75% -11.47% -11.5% 0.97%
#> 5 2.31% 4.31% -8.38% 9.91%
#> 6 4.63% 4.52% 4.82% -20.52%
#> 7 7.27% -5.72% 33.47% 4.81%
#> 8 1.72% 6.92% 9.11% 15.57%
#> 9 0.4% 2.59%
#Excluding Benchmark Returns
df<-mperf_table(da[,c("date","asset_ret")],ts_format="returns",print_output=F)
# Using Index instead of Return Time Series
date=seq(as.Date("2010-1-1"), as.Date("2015-1-1"), by = "days")
asset_ret<-cumprod(1+rnorm(length(date))/100)
benchmark_ret<-cumprod(1+rnorm(length(date))/100)
da<-data.frame(date,asset_ret,benchmark_ret)
df<-mperf_table(da,ts_format="index",print_output=F)
# You can also configure the export options and download the plotly graphic as a high resolution svg
df<-mperf_table(da,ts_format="index",header_color="#3b5171",font_color="#04103b",export_format="svg",
chart_export_width=600,chart_export_height=150,print_output=F)
# Display Plotly Graphic
df$fig
Apply NBER recession shading to any ggplot2 time series graphic, directly loading the recession indicator from the St Louis Fed. Please note, that this function requires an API key that can be generated for free on the Fed’s website (https://fred.stlouisfed.org/docs/api/api_key.html).
Analyse frequence, magnitude and length of drawdowns in a given time series. Please note: If the time series ends in a drawdown, the last peak2recovery and trough2recovery values display the number of periods since the last trough and the date of the last observation even if this does not mark a complete recovery.
# Generate a random return time series
date=seq(as.Date("2010-1-1"), as.Date("2015-1-1"), by = "days")
asset_ret<-rnorm(length(date))/100
da<-data.frame(date,asset_ret)
# Analyze the number, magnitude and length of drawdowns
df<-dRawdowns(da,ret_format='returns',graphics=F)
# Some example output
df$longest_drawdown
#> Time difference of 383 days
df$longest_peak2trough
#> Time difference of 238 days
# Count number of drawdowns with a trough below threshold value
df$n
#> ranges observations
#> 1 0.00 67
#> 2 -0.05 8
#> 3 -0.10 4
#> 4 -0.20 0
#> 5 -0.30 0
#> 6 -0.40 0
#> 7 -0.50 0
#> 8 0.60 0
#> 9 -0.70 0
sequence of random returns ## Example application rrScat & rrScatEff functions
The rrScat functions provide visual insights into the risk/return profile of a given sample of assets. rrScat displays the annualized risk (standard deviation) of returns and the annualized return in a scatter plot. rrScatEff provides an interface to the ROI optimizer of the PortfolioAnalytics package and plots the efficient frontier alongisde the given assets.
# Generate a dataframe of random asset returns
date=seq(as.Date("2010-1-1"), as.Date("2015-1-1"), by = "days")
Asset1<-rnorm(length(date))/100+0.0005
Asset2<-rnorm(length(date))/100+0.0005
Asset3<-rnorm(length(date))/100+0.0005
Asset4<-rnorm(length(date))/100+0.0005
da<-data.frame(date,Asset1,Asset2,Asset3,Asset4)
# Plot the risk/return scatter
df<-rrScat(da,ret_format="returns",table_format='wide')
df$rr_ggplot
# Plot the risk/return scatter and the efficient frontier
p<-rrScatEff(da,ret_format="returns",table_format='wide')
# The function can also handle data in long format.
# For this, the data should be arranged as follows: date, id, values
long <- melt(setDT(da), id.vars = "date")
eff<-rrScatEff(long,ret_format="returns",table_format='long')
eff$eff_ggplot
Investors are frequently exposed to the risk of foreign exchange rate changes. The very good underlying performance of an investment, demoninated in a foreign currency, can be (over)compensated easily by a depreciation in the respective currency’s value compared to the investor’s home currency. For this reason, investors regularly insure themselves (hedge) against this risk using Swaps or Forward contracts. However, the price of these hedges depends on the relative interest rates level. As a rule of thumb, investor’s can’t escape their home country’s interest rate environment without taking foreign exchange risk. If interest rates in the foreign currency are higher, ceteris paribus, hedging will cost money and vice versa if foreign interest rates are lower. The cost of a hedge can, therefore, be simulated easily using the interest rate differential. The FXhedgeR provides a generic function that calculates the cost of carry of the hedge of any currency pair based on two dataframes of historical FX rates and forwards points (both can be obtained for instance from Bloomberg). For major currencies, the function uses an example dataset hosted on GitHub by default which, however, may not be up-to-date.
# Obtain the carry paid/earned by hedging USD exposure for a Singapore based investor
# A negative figure means, the hedge costs money, a postive figure indicates that the investor is earning carry.
df<-FXhedgeR(base_currency='SGD',exp_currency='USD')
tail(df$forwards_perc,1)
#> Dates Hedge
#> 1: 2023-03-07 0.013948
# Obtain the carry paid/earned by hedging GBP exposure for a Swiss investor with user-given data
df<-
FXhedgeR(base_currency='CHF',exp_currency='GBP',
fxrates_gui=read_delim(url('https://raw.githubusercontent.com/fscheler/opendata/main/fx_rates.csv'),delim = ";",show_col_types = FALSE),
fxforwards_gui=read_delim(url('https://raw.githubusercontent.com/fscheler/opendata/main/fx_forwards.csv'),delim = ";",show_col_types = FALSE)
)
head(df$hedge_perf)
#> Dates Hedge
#> 1: 1999-05-20 1.0001607
#> 2: 1999-05-21 1.0003214
#> 3: 1999-05-24 1.0060352
#> 4: 1999-05-25 1.0099296
#> 5: 1999-05-26 0.9983723
#> 6: 1999-05-27 0.9966876
tail(df$forwards_perc)
#> Dates Hedge
#> 1: 2023-02-28 0.03138301
#> 2: 2023-03-01 0.03035254
#> 3: 2023-03-02 0.02991483
#> 4: 2023-03-03 0.02987529
#> 5: 2023-03-06 0.02821794
#> 6: 2023-03-07 0.02795312
tail(df$cumulative_hedge_cost)
#> Dates Hedge
#> 1: 2023-02-28 1.650495
#> 2: 2023-03-01 1.650694
#> 3: 2023-03-02 1.650890
#> 4: 2023-03-03 1.651085
#> 5: 2023-03-06 1.651270
#> 6: 2023-03-07 1.651453
The package depends on the following packages:
lubridate, fredr, ecm, ggplot2, dplyr, scales, fredr, purrr, PortfolioAnalytics, ecm, plotly, tidyverse, caTools, zoo, data.table, tibble